/**
 * 模拟Promise.prototype.finally
 * 原型方法，无论成功还是失败都会执行回调
 * @param {Function} onFinally
*/

Promise.prototype.myFinally = function(onFinally) {
  return this.then(
    (res) => Promise.resolve(onFinally()).then(() => res),
    (err) => Promise.resolve(onFinally()).then(() => { throw err; })
  )
}

// // 测试
// Promise.resolve(123)
//   .then((res) => {
//     console.log(res); //123
//     return Promise.reject(456);
//   })
//   .myFinally(() => {
//     console.log("finally"); // finally
//     return "finally本身不返回值";
//   })
//   .then(
//     (res) => {},
//     (err) => {
//       console.log(err); // 输出456 (myFinally不会改变promise状态，当前状态rejected) 
//       return 789;
//     }
//   )
//   .myFinally(() => console.log("finally")) // finally
//   .then((res) => console.log(res)); //789
